package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.view.CustomChooseView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by zw on 2016/3/1 0001.
 * 商户状态 状态 1待一审  2待平台审核  3审核失败  4正常
 */
public class MerchantStateActivity extends ABBaseActivity implements View.OnClickListener {

    //全部-1;0关闭;1正常;2 冻结
    //进件状态 1待一审  2待平台审核  3审核失败  4正常
    private String[] states = {"待一审", "待平台审核", "审核失败", "正常"};
    private int[] ids = {R.id.choose_view_1, R.id.choose_view_2,R.id.choose_view_3,R.id.choose_view_4};
//    ,R.id.choose_view_5,R.id.choose_view_6,R.id.choose_view_7
    private TitleBar titleBar;
    private CustomChooseView[] chooseViews;//所有的状态
    private RelativeLayout tv_all;
    private ImageView iv_all;
    private boolean isSingle = true;// 是否是单选
    private boolean isAll = false;//是否全选

    Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_merchant_state;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
//        titleBar.setShowRight(View.VISIBLE);
        //add by zhuangzeqin 2017年4月24日11:22:13  隐藏右边的确定按钮
        titleBar.setShowRight(View.GONE);
        titleBar.setRightTextView("确定");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        tv_all = getViewById(R.id.layout_all);
        iv_all = getViewById(R.id.iv_all);
        chooseViews = new CustomChooseView[states.length];
        for (int i = 0; i < states.length; i++) {
            chooseViews[i] = getViewById(ids[i]);
            chooseViews[i].setTitle(states[i]);
            chooseViews[i].setTag(i);
            chooseViews[i].setOnClickListener(this);
        }

        if(isSingle){
            tv_all.setVisibility(View.GONE);
        }
    }

    @Override
    protected void eventOnClick() {
        tv_all.setOnClickListener(this);
        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                getSelectByStateForResult();
            }
        });
    }

    /**
     * 获取选中的状态
     */
    private void getSelectByStateForResult() {
        intent = new Intent();
        if(isAll){
            intent.putExtra("isAll", isAll);
        }else{
            String state = "";
            for (CustomChooseView cv: chooseViews) {
                if(cv.getIsSelected()){
                   state += (cv.getLeftTitle());
                }
            }
            intent.putExtra("state", state);
            String statusCode = "";
            switch (state){
                case "待一审":
                    statusCode = "1";
                    break;
                case "待平台审核":
                    statusCode = "2";
                    break;
                case "审核失败":
                    statusCode = "3";
                    break;
                case "正常":
                    statusCode = "4";
                    break;
            }
            intent.putExtra("stateCode", statusCode);
        }
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_all:
                changeState();
                break;
            default:
                if(v.getTag() != null){
                    int index = (int) v.getTag();
                    singleSelect(index);
                }
                //add by zhuangzeqin 获取选中的状态；直接返回；无需点击确定按钮
                getSelectByStateForResult();
                break;
        }
    }

    /**
     * 单选
     * @param index
     */
    private void singleSelect(int index){
        if(isSingle){
            for (int i = 0; i < chooseViews.length; i++){
                CustomChooseView childView = chooseViews[i];
                if(i == index){
                    childView.setIsSelected(true);
                }else{
                    childView.setIsSelected(false);
                }
            }
        }

    }

    /**
     * 选中全部的时候改变其他状态为不选中
     */
    private void changeState(){
        isAll = !isAll;
        if(isAll){
            iv_all.setVisibility(View.VISIBLE);
            for (CustomChooseView cv: chooseViews) {
                cv.setIsSelected(false);
            }
        }else{
            iv_all.setVisibility(View.GONE);
        }
    }




}
